using System;
using System.IO;
using ETModel;
using PlayerSession;

namespace ETHotfix
{
    public static class LoginHelper
    {
        public static async ETVoid OnLoginAsync(string account, string password)
        {
            try
            {
                UI loginUI = Game.Scene.GetComponent<UIComponent>().Get(UIType.UILogin);
                UILoginComponent loginComponent = loginUI.GetComponent<UILoginComponent>();

                // 创建一个ETModel层的Session
                ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);

                // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
                Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
                realmSession.session.GetComponent<SessionCallbackComponent>().DisposeCallback += s =>
                {
                    if (Game.Scene.GetComponent<UIComponent>()?.Get(UIType.UILogin) != null)
                    {
                        loginComponent.SetPrompt("断开连接");
                    }
                };
                loginComponent.SetPrompt("正在登录中....");

                SCPlayerLogin r2CLogin = (SCPlayerLogin)await realmSession.Call(new CSPlayerLogin() { AccountID = int.Parse(account), Password = password });
                realmSession.Dispose();
                if(r2CLogin.ErrorCode == 100)
                {
                    loginComponent.SetPrompt("玩家已登录！");
                }
                else if (r2CLogin.ErrorCode == 101)
                {
                    loginComponent.SetPrompt("账号不存在！");
                }
                else if (r2CLogin.ErrorCode == 102)
                {
                    loginComponent.SetPrompt("密码错误！");
                }
                else
                {
                    loginComponent.SetPrompt("");
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static async ETVoid OnRegisterAsync(string account, string password)
        {
            try
            {
                {
//                     // 创建一个ETModel层的Session
//                     ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
// 
//                     // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
//                     Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
// 
//                     R2C_Register_Ack r2CLogin = (R2C_Register_Ack)await realmSession.Call(new C2R_Register_Req() { Account = account, Password = password });
// 
//                     if (r2C_Register_Ack.Error == ErrorCode.ERR_AccountAlreadyRegister)
//                     {
//                         prompt.text = "注册失败，账号已被注册";
//                         account.text = "";
//                         password.text = "";
//                         return;
//                     }
// 
//                     OnLoginAsync(account, password).Coroutine();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
    }
}